﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using Cqrs.Common;

namespace Cqrs.StateMachineDesigner.CustomCode
{
    /// <summary>
    /// Interaction logic for DomainProperties.xaml
    /// </summary>
    public partial class DomainProperties : Window
    {
        public DomainProperties()
        {
            InitializeComponent();
        }

        private DomainPropertyModel _domainPropertyModel;

        List<string> _dataTypes = new List<string>(){
            "byte","bool","char","DateTime", "decimal","double","float","Guid", "int",
            "long","object","sbyte","short","string","uint","ulong",
            "ushort"
        };


        public DomainPropertyModel ShowForm(DomainPropertyModel domainPropertiesModel)
        {
            _domainPropertyModel = domainPropertiesModel;
            grdProperties.DataContext = _domainPropertyModel.DomainProperties;
                txtName.Text = _domainPropertyModel.Name;
                txtNamespace.Text = _domainPropertyModel.Namespace;
            cboTypes.ItemsSource = _dataTypes;
            if (txtName.Text == "[untitled]")
            {
                txtName.Focus();
                txtName.SelectAll();
            }
            this.ShowDialog();
            return _domainPropertyModel;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _domainPropertyModel.DomainProperties.Add(new DomainProperty() { 
                        Name = txtPopertyName.Text, 
                        Type = cboTypes.Text, 
                        ApplyToAllCommandsEvents = chkApplyToAll.IsChecked.Value});
                grdProperties.Items.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (grdProperties.SelectedItems.Count == 0) return;
                foreach (DomainProperty prop in grdProperties.SelectedItems)
                {
                    _domainPropertyModel.DomainProperties.Remove(prop);
                }
                grdProperties.Items.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            _domainPropertyModel = null;
            this.Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtName.Text))
                {
                    MessageBox.Show("A Domain Name must be specified.");
                    return;
                }
                _domainPropertyModel.Name = txtName.Text;
                _domainPropertyModel.Namespace = txtNamespace.Text;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public class DomainPropertyModel
        {
            public string Name { get; set; }
            public string Namespace { get; set; }
            public List<DomainProperty> DomainProperties { get; set; } 
        }
    }
}
